package com.uzai.console.web.profit;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.profit.ladderratio.LadderRatioIdDto;
import com.uzai.console.dto.profit.ladderratio.LadderRatioIdListDto;
import com.uzai.console.dto.profit.ladderratio.LadderRatioInfoDto;
import com.uzai.console.dto.profit.ladderratio.LadderRatioQuery;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupIdDto;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupInfoDto;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.entity.LadderRatioGroup;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.LadderRatioGroupMapper;
import com.uzai.console.service.profit.LadderRatioService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.profit.ladderratio.LadderRatioInfoVo;
import com.uzai.console.vo.profit.ladderratiogroup.LadderRatioGroupInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:15
 */
@Api(value = "阶梯规则",tags = "分佣管理|阶梯规则")
@ApiSort(4)
@RestController
public class LadderRatioContoller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(LadderRatioContoller.class);

    @Autowired
    private LadderRatioService ladderRatioService;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;

    @ApiOperation(value = "分页查询阶梯规则列表", notes = "分页查询阶梯规则列表", response = LadderRatioInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/ladderRatio/findLadderRatioList")
    public Object findLadderRatioList(@RequestBody LadderRatioQuery query){
        return ladderRatioService.findLadderRatioList(query);
    }

    @ApiOperation(value = "查询阶梯规则详情", notes = "查询阶梯规则详情", response = LadderRatioInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/ladderRatio/findLadderRatioInfo")
    public Object findLadderRatioInfo(@RequestBody LadderRatioIdDto ladderRatioIdDto){
        return ladderRatioService.findLadderRatioInfo(ladderRatioIdDto);
    }

    @ApiOperation(value = "添加阶梯规则前检验接口", notes = "添加阶梯规则前检验接口")
    @ApiOperationSupport(order = 3)
    @PostMapping("/ladderRatio/addLadderRatioUnit")
    public Object addLadderRatioUnit(){
        LadderRatioGroupQuery ladderRatioGroupQuery = new LadderRatioGroupQuery();
        ladderRatioGroupQuery.setMerId(getLoginInfo().getMerId());
        List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGroupQuery);
        if(ladderRatioGroupList == null || ladderRatioGroupList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先添加分组信息");
        }
        return true;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加阶梯规则", notes = "添加阶梯规则")
    @ApiOperationSupport(order = 3)
    @PostMapping("/ladderRatio/addLadderRatio")
    public Object addLadderRatio(@RequestBody LadderRatioInfoDto ladderRatioInfoDto){
        ladderRatioService.addLadderRatio(ladderRatioInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改阶梯规则", notes = "修改阶梯规则")
    @ApiOperationSupport(order = 4)
    @PostMapping("/ladderRatio/updateLadderRatio")
    public Object updateLadderRatio(@RequestBody LadderRatioInfoDto ladderRatioInfoDto){
        ladderRatioService.updateLadderRatio(ladderRatioInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除阶梯规则", notes = "删除会员类m型")
    @ApiOperationSupport(order = 5)
    @PostMapping("/ladderRatio/deleteLadderRatio")
    public Object deleteLadderRatio(@RequestBody LadderRatioIdDto ladderRatioIdDto){
        ladderRatioService.deleteLadderRatio(ladderRatioIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "批量删除阶梯规则", notes = "批量删除会员类m型")
    @ApiOperationSupport(order = 5)
    @PostMapping("/ladderRatio/deleteLadderRatio/batch")
    public Object deleteLadderRatioBatch(@RequestBody LadderRatioIdListDto ladderRatioIdListDto){
        ladderRatioService.deleteLadderRatioBatch(ladderRatioIdListDto);
        return true;
    }

    @ApiOperation(value = "分页查询阶梯规则分组列表", notes = "分页查询阶梯规则分组列表", response = LadderRatioGroupInfoVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/ladderRatio/findLadderRatioGroupList")
    public Object findLadderRatioGroupList(@RequestBody LadderRatioGroupQuery ladderRatioGroupQuery){
        return ladderRatioService.findLadderRatioGroupList(ladderRatioGroupQuery);
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加阶梯规则分组", notes = "添加阶梯规则分组")
    @ApiOperationSupport(order = 7)
    @PostMapping("/ladderRatio/addLadderRatioGroup")
    public Object addLadderRatioGroup(@RequestBody LadderRatioGroupInfoDto ladderRatioGroupInfoDto){
        ladderRatioService.addLadderRatioGroup(ladderRatioGroupInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除阶梯规则分组", notes = "删除阶梯规则分组")
    @ApiOperationSupport(order = 8)
    @PostMapping("/ladderRatio/deleteLadderRatioGroup")
    public Object deleteLadderRatioGroup(@RequestBody LadderRatioGroupIdDto ladderRatioGroupIdDto){
        ladderRatioService.deleteLadderRatioGroup(ladderRatioGroupIdDto);
        return true;
    }

    @ApiOperation(value = "查询阶梯规则分组下拉列表框", notes = "查询阶梯规则分组下拉列表框", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/ladderRatio/findLadderRatioGroupSelect")
    public Object findLadderRatioGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        LadderRatioGroupQuery ladderRatioGroupQuery =  new LadderRatioGroupQuery();
        ladderRatioGroupQuery.setMerId(getLoginInfo().getMerId());
        List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGroupQuery);
        if(ladderRatioGroupList != null && ladderRatioGroupList.size() > 0){
            for(LadderRatioGroup ladderRatioGroup : ladderRatioGroupList){
                SelectKeyVo selectKeyVo =  new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(ladderRatioGroup.getId()));
                selectKeyVo.setValue(ladderRatioGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改阶梯规则分组", notes = "修改阶梯规则分组")
    @ApiOperationSupport(order = 10)
    @PostMapping("/ladderRatio/updateLadderRatioGroup")
    public Object updateLadderRatioGroup(@RequestBody LadderRatioGroupInfoDto ladderRatioGroupInfoDto){
        ladderRatioService.updateLadderRatioGroup(ladderRatioGroupInfoDto);
        return true;
    }


    @SystemLogType(model = SystemModelEnum.LadderRatio, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "复制阶梯分组", notes = "复制阶梯分组")
    @ApiOperationSupport(order = 11)
    @PostMapping("/ladderRatio/copyLadderRatioByGroup")
    public Object copyLadderRatioByGroup(@RequestBody LadderRatioGroupInfoDto ladderRatioGroupInfoDto){
        ladderRatioService.copyLadderRatioByGroup(ladderRatioGroupInfoDto);
        return true;
    }
}
